package client.comm.baoding.dialog

import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.databinding.Observable
import androidx.databinding.ObservableField
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import client.comm.baoding.R
import client.comm.baoding.api.bean.Addr
import client.comm.baoding.api.bean.GylAddrRet
import client.comm.baoding.databinding.DialogSelectgyladdrLayoutBinding
import client.comm.baoding.ui.viewmodel.MiddleVm
import client.comm.commlib.network.data.JsonResult
import client.comm.commlib.widget.EmptyLayout
import client.tzstore.oo.dialog.BottomDialog
import client.tzstore.oo.dialog.adapter.SelectGylAddrAdapter

class SelectGylAddrDialog(val context: FragmentActivity, val vm: MiddleVm) : BottomDialog(context) {

    val adapter: SelectGylAddrAdapter by lazy {
        SelectGylAddrAdapter(context).apply {
            event = {
                when (it.level) {
                    1 -> {
                        selectOneAddr.set(it)
                        curParent.set(it.code)
                        curLevel.set("${it.level + 1}")

                        binding.twoTab.isSelected = true
                        binding.oneTab.isSelected = false
                        binding.threeTab.isSelected = false
                        binding.fourTab.isSelected = false

                        if (selectTwoAddr.get() != null && selectTwoAddr.get()!!.parentId.equals(
                                selectOneAddr.get()?.code
                            )
                        ) {

                        } else {
                            selectTwoAddr.set(null)
                            selectThreeAddr.set(null)
                            selectFourAddr.set(null)

                            binding.twoTab.visibility = View.INVISIBLE
                            binding.threeTab.visibility = View.INVISIBLE
                            binding.fourTab.visibility = View.INVISIBLE

                        }


                        holdCall()
                    }

                    2 -> {
                        selectTwoAddr.set(it)
                        curParent.set(it.code)
                        curLevel.set("${it.level + 1}")

                        binding.threeTab.isSelected = true
                        binding.twoTab.isSelected = false
                        binding.oneTab.isSelected = false
                        binding.fourTab.isSelected = false

                        if (selectThreeAddr.get() != null && selectThreeAddr.get()!!.parentId.equals(
                                selectTwoAddr.get()?.code
                            )
                        ) {

                        } else {
                            binding.threeTab.visibility = View.INVISIBLE
                            binding.fourTab.visibility = View.INVISIBLE

                            selectThreeAddr.set(null)
                            selectFourAddr.set(null)
                        }

                        holdCall()
                    }

                    3 -> {
                        selectThreeAddr.set(it)
                        curParent.set(it.code)
                        curLevel.set("${it.level + 1}")

                        binding.fourTab.isSelected = true
                        binding.twoTab.isSelected = false
                        binding.oneTab.isSelected = false
                        binding.threeTab.isSelected = false

                        if (selectFourAddr.get() != null && selectFourAddr.get()!!.parentId.equals(
                                selectThreeAddr.get()?.code
                            )
                        ) {

                        } else {
                            binding.fourTab.visibility = View.INVISIBLE
                            selectFourAddr.set(null)
                        }

                        holdCall()
                    }

                    4 -> {
                        selectFourAddr.set(it)
                        ondismis?.invoke(
                            selectOneAddr.get()!!,
                            selectTwoAddr.get()!!,
                            selectThreeAddr.get()!!,
                            selectFourAddr.get()
                        )
                    }
                }

            }
        }
    }
    val listRet = MutableLiveData<JsonResult<List<Addr>>>()

    val binding by lazy {
        DataBindingUtil.inflate<DialogSelectgyladdrLayoutBinding>(
            LayoutInflater.from(context),
            R.layout.dialog_selectgyladdr_layout,
            null,
            false
        )
    }

    val curParent = ObservableField<String>("0")
    val curLevel = ObservableField<String>("1")

    val selectOneAddr by lazy {
        ObservableField<Addr?>().apply {
            addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
                override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
                    binding.oneTv.text = if (get() == null) "请选择" else get()!!.name
                }
            })
        }
    }
    val selectTwoAddr by lazy {
        ObservableField<Addr?>().apply {
            addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
                override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
                    binding.twoTv.text = if (get() == null) "请选择" else get()!!.name
                }
            })
        }
    }
    val selectThreeAddr by lazy {
        ObservableField<Addr?>().apply {
            addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
                override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
                    binding.threeIv.text = if (get() == null) "请选择" else get()!!.name
                }
            })
        }
    }
    val selectFourAddr by lazy {
        ObservableField<Addr?>().apply {
            addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
                override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
                    binding.fourTv.text = if (get() == null) "请选择" else get()!!.name
                }
            })
        }
    }

    override fun getV(): View {
        binding.event = this
        binding.recyclerView.adapter = adapter

        binding.emptyLayout.setOnClickListener {
            holdCall()
        }
        listRet.observe(context, Observer {
            it?.let {
                if (it.isSuccess) {

                    adapter.select = -1
                    adapter.selectId = ""
                    binding.emptyLayout.visibility = View.GONE
                    when (curLevel.get()) {
                        "1" -> {
                            it.data?.forEach {
                                it.level = 1
                            }
                            binding.oneTab.visibility = View.VISIBLE

                            selectOneAddr.get()?.let {
                                adapter.selectId = it.code
                            }

                        }

                        "2" -> {

                            it.data?.forEach {
                                it.level = 2
                            }
                            binding.twoTab.visibility = View.VISIBLE

                            selectTwoAddr.get()?.let {
                                adapter.selectId = it.code
                            }
                        }

                        "3" -> {
                            it.data?.forEach {
                                it.level = 3
                            }
                            binding.threeTab.visibility = View.VISIBLE

                            selectThreeAddr.get()?.let {
                                adapter.selectId = it.code
                            }
                        }

                        "4" -> {
                            it.data?.forEach {
                                it.level = 4
                            }
                            if (it.data!!.size > 0) {
                                binding.fourTab.visibility = View.VISIBLE
                                selectFourAddr.get()?.let {
                                    adapter.selectId = it.code
                                }
                            } else {
                                ondismis?.invoke(
                                    selectOneAddr.get()!!,
                                    selectTwoAddr.get()!!,
                                    selectThreeAddr.get()!!,
                                    null
                                )
                            }
                        }
                    }
                    val list = it.data

                    adapter.submitList(list)

                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
        return binding.root
    }

    fun showDialog(
        one: String,
        oneId: String,
        two: String,
        twoId: String,
        three: String,
        threeId: String,
        four: String,
        fourId: String,
    ) {

        if(!one.isNullOrEmpty()){
            selectOneAddr.set(
                Addr(
                    code = oneId,
                    level = 1,
                    name = one,
                    parentId = "0"
                )
            )
            selectTwoAddr.set(
                Addr(
                    code = twoId,
                    level = 2,
                    name = two,
                    parentId = oneId
                )
            )
            selectThreeAddr.set(
                Addr(
                    code = threeId,
                    level = 3,
                    name = three,
                    parentId = twoId
                )
            )
            selectFourAddr.set(
                Addr(
                    code = fourId,
                    level = 4,
                    name = four,
                    parentId = threeId
                )
            )

            binding.oneTab.visibility = View.VISIBLE
            binding.twoTab.visibility = View.VISIBLE
            binding.threeTab.visibility = View.VISIBLE
            binding.fourTab.visibility = if (fourId.equals("0")) View.INVISIBLE else View.VISIBLE
            if (fourId.equals("0")) {
                binding.threeTab.isSelected = true
            } else
                binding.fourTab.isSelected = true

            curParent.set(if (fourId.equals("0")) twoId else threeId)
            curLevel.set(if (fourId.equals("0")) "3" else "4")
        }

        holdCall()

        show()
    }

    fun holdCall() {
        vm.hold({
            //根据类型，请求不同银行卡列表
            vm.server.get_level_addrlist(curParent.get()!!)
        }, {
            binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
        }, success = {
            listRet.value = it
        }, complete = {
        })
    }


    fun tabClick(view: View) {
        val tag = view.tag
        if (view.isSelected)
            return
        when (tag) {
            "0" -> {
                view.isSelected = true

                curLevel.set("1")
                curParent.set("0")

                binding.twoTab.isSelected = false
                binding.threeTab.isSelected = false
                binding.fourTab.isSelected = false
            }

            "1" -> {

                curLevel.set("2")
                curParent.set(selectOneAddr.get()?.code)
                view.isSelected = true
                binding.oneTab.isSelected = false
                binding.threeTab.isSelected = false
                binding.fourTab.isSelected = false
            }

            "2" -> {
                curLevel.set("3")
                curParent.set(selectTwoAddr.get()?.code)

                view.isSelected = true
                binding.twoTab.isSelected = false
                binding.oneTab.isSelected = false
                binding.fourTab.isSelected = false
            }

            "3" -> { //街道
                curLevel.set("4")
                curParent.set(selectThreeAddr.get()?.code)

                view.isSelected = true
                binding.twoTab.isSelected = false
                binding.oneTab.isSelected = false
                binding.threeTab.isSelected = false
            }
        }

        holdCall()
    }

    var ondismis: ((one: Addr, two: Addr, three: Addr, four: Addr?) -> Unit)? = null


}